import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/Emergency_event_process_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_list_entity.dart';
import 'package:flutter/cupertino.dart';

/// 救援过程
class RescueProcess extends StatefulWidget {
  const RescueProcess({super.key});

  @override
  State<RescueProcess> createState() => _RescueProcessState();
}

class _RescueProcessState extends State<RescueProcess> {
  /// 事故事件数据
  EmergencyEventListEntity item = Get.arguments as EmergencyEventListEntity;

  // 救援过程数据
  EmergencyEventProcessEntity rescueProcessData = EmergencyEventProcessEntity();

  // 获取救援过程数据
  void getRescueProcessData() async {
    final res = await HttpUtil().generateGetRequest<EmergencyEventProcessEntity>(
      Interface.getAccidentInfoDetail,
      queryParameters: {
        'id': item.id,
      },
    ).execute();

    res.onSuccess((result) {
      if (result.data != null) {
        rescueProcessData = result.data!;
        if(mounted) {
          setState(() {});
        }
      }
    });
  }

  @override
  void initState() {
    super.initState();
    getRescueProcessData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Container(
            height: AppDimens.dp12,
            color: AppColors.bg_color_grey,
          ),
          Container(
            height: AppDimens.dp2,
            color: AppColors.blue_button,
          ),
          Container(
            height: AppDimens.dp40,
            width: AppDimens.sw,
            color: const Color(0xFFF4F7FF),
            alignment: Alignment.centerLeft,
            child: Container(margin: EdgeInsets.only(left: AppDimens.dp40), child: Text('事故名称：${item.accidentName}', style: AppTextStyles.black_16)),
          ),
          _buildBasicInfoItem('记录人', rescueProcessData.recordBy, true),
          _buildBasicInfoItem('记录时间', rescueProcessData.recordTime, true),
          _buildBasicInfoItem('调用物资人员情况', rescueProcessData.rescueGoodsPeople.toString(), true),
          _buildBasicInfoItem('涉及危险化学品', '${item.chemicalTypeNameList}', true),
          _buildBasicInfoItem('备注', rescueProcessData.rescueRemarks, false),
          Row(
            children: [
              SizedBox(
                width: 37.w,
              ),
              Text(
                '救援会议纪要',
                style: TextStyle(
                  fontSize: 14.sp,
                  color: const Color(0xFF7C7D93),
                ),
              ),
              SizedBox(
                width: 35.w,
              ),
              CupertinoButton(
                padding: EdgeInsets.zero,
                child: Text(rescueProcessData.rescueMeetingRecord ?? '暂无'),
                onPressed: () {
                  FileUtils.openFile(rescueProcessData.rescueMeetingRecord);
                },
              )
            ],
          )
        ],
      ),
    );
  }

  /// 基本信息
  Widget _buildBasicInfoItem(
    String key,
    String? val,
    bool isRequired, {
    bool isPic = false,
  }) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 37.w, vertical: 6.w),
      child: Row(
        children: [
          SizedBox(
            width: 130.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                if (isRequired)
                  Text(
                    '*',
                    style: TextStyle(
                      fontSize: 13.sp,
                      color: Colors.red,
                    ),
                    textAlign: TextAlign.center,
                  ),
                Text(
                  key,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xFF7C7D93),
                  ),
                )
              ],
            ),
          ),
          isPic
              ? SizedBox(
                  height: 77.w,
                  width: 77.w,
                  child: Image.network(val ?? ''),
                )
              : Text(
                  val ?? '空',
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: const Color(0xFF495061),
                  ),
                ),
        ],
      ),
    );
  }
}
